<?php

global $_W, $_GPC;
$_GPC['do'] = 'post';
$_GPC['menu'] = 'data';

if ($_GPC['act'] == 'mutdelete') {
    $ids = $_GPC['ids'];
    foreach ($ids as $id) {
        M('post')->delete($id);
    }
    message('删除成功', referer(), 'success');
}


if ($_GPC['act'] == 'edit') {
    $id = intval($_GPC['id']);
    if ($_W['ispost']) {

        $data = array();
        $data['uniacid'] = $_W['uniacid'];

        if (isset($_GPC['category_id'])) {
            $data['category_id'] = intval($_GPC['category_id']);
        }

        if (isset($_GPC['knowledge_id'])) {
            $data['knowledge_id'] = intval($_GPC['knowledge_id']);
        }

        if (isset($_GPC['content'])) {
            $data['content'] = trim($_GPC['content']);
        }
        $answerType = $_GPC['answer_type'];
        if ($answerType == 1) {
            $data['item'] = iserializer($_GPC['radio']);
            $data['answer_item'] = iserializer($_GPC['radio_answer']);
        } elseif ($answerType == 2) {
            $data['item'] = iserializer($_GPC['checkbox']);
            $data['answer_item'] = iserializer($_GPC['checkbox_answer']);
        } elseif ($answerType == 3) {

        } else {
            itoast('请选择类型', 'referer', 'error');
        }

        $data['answer_content'] = $_GPC['answer_content'];
        $data['answer_type'] = $answerType;
        $data['differ'] = $_GPC['differ'];

        $data['create_time'] = time();
        if (!empty($id)) {
            $data['id'] = $id;
            unset($data['create_time']);
            $post = M('post')->update($data);
        } else {
            $post = M('post')->update($data);
        }
        itoast('保存成功', $this->createWebUrl('post'), 'success');
    }
    $item = M('post')->getInfo($id);
    $category = M('category')->getall();
    $user = M('user')->getInfo($item['user_id']);
    include $this->template('post_edit');
    exit;
}
if ($_GPC['act'] == 'delete') {
    $id = intval($_GPC['id']);
    if (empty($id)) {
        if ($_W['ispost']) {
            $data = array();
            $data['status'] = 1;
            $data['message'] = '参数错误';
            die(json_encode($data));
        } else {
            message('参数错误', referer(), 'error');
        }
    }
    M('post')->delete($id);
    if ($_W['ispost']) {
        $data = array();
        $data['status'] = 1;
        $data['message'] = '操作成功';
        die(json_encode($data));
    } else {
        message('删除成功', referer(), 'success');
    }
}
// 帖子列表
$page = !empty($_GPC['page']) ? intval($_GPC['page']) : 1;
$where = "";
$params = array();
if (!empty($_GPC['category_id'])) {
    $where .= " AND category_id = :category_id ";
    $params[':category_id'] = intval($_GPC['category_id']);
}
if ($_GPC['is_pass'] != '') {
    $where .= " AND is_pass = :is_pass ";
    $params[':is_pass'] = intval($_GPC['is_pass']);
}

if (!empty($_GPC['content'])) {
    $where .= " AND content like '%" . trim($_GPC['content']) . "%' ";
}

$list = M('post')->getList($page, $where, $params);
$answerTypeArr = array(
    1 => '单选',
    2 => '多选',
    3 => '文本'
);
if (!empty($list)) {
    foreach ($list['list'] as $key => $value) {
        // 发布内容
        $list['list'][$key]['content'] = M('common')->handle_content(strip_tags(htmlspecialchars_decode($value['content'])), 20);
        // 分类
        $category = M('category')->getInfo($value['category_id']);
        $list['list'][$key]['category_name'] = $category['name'];
        // 选项类型
        $list['list'][$key]['answer_type'] = $answerTypeArr[$value['answer_type']];
        // 时间
        $list['list'][$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
    }
}

// 一级知识点
if ($_GPC['act'] == 'category') {
    $where = array(
        'category_id' => $_GPC['category_id'],
        'fid' => 0
    );
    $category = M('knowledge')->getall($where);
    echo json_encode($category);
    exit;
}

// 二级知识点
if ($_GPC['act'] == 'knowledge') {
    $where = array(
        'fid' => $_GPC['fid']
    );
    $category = M('knowledge')->getall($where);
    echo json_encode($category);
    exit;
}

// 帖子统计
$total = M('post')->totalCredit();

// 分类
$category = M('category')->getall();

include $this->template('post');